//src\router\index.js
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { useUserStore } from '../stores/userStore'

// 统一使用History模式，更符合生产环境标准
const routerMode = createWebHistory

const routes = [
  {
    path: '/test',
    name: 'test',
    component: () => import('@/views/TestPage.vue'),
    meta: { 
      title: '测试页面',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/simple-test',
    name: 'simple-test',
    component: () => import('@/views/SimpleTest.vue'),
    meta: { 
      title: '简单测试页面',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/minimal-test',
    name: 'minimal-test',
    component: () => import('@/views/MinimalTest.vue'),
    meta: { 
      title: '最小化测试页面',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/standalone-test',
    name: 'standalone-test',
    component: () => import('@/views/StandaloneTest.vue'),
    meta: { 
      title: '独立测试页面',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/debug-test',
    name: 'debug-test',
    component: () => import('@/views/DebugTest.vue'),
    meta: { 
      title: '调试测试页面',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/production-debug',
    name: 'production-debug',
    component: () => import('@/views/ProductionDebug.vue'),
    meta: { 
      title: '生产环境诊断',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/nginx-debug',
    name: 'nginx-debug',
    component: () => import('@/views/NginxDebug.vue'),
    meta: { 
      title: 'Nginx环境诊断',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/api-debug',
    name: 'api-debug',
    component: () => import('@/views/ApiDebug.vue'),
    meta: { 
      title: 'API连接调试',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/sidebar-test',
    name: 'sidebar-test',
    component: () => import('@/views/SidebarTest.vue'),
    meta: { 
      title: '侧边栏极简测试',
      closable: false,
      requiresAuth: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    meta: { 
      title: '登录',
      closable: false
    }
  },
  {
    path: '/',
    name: 'app',
    component: () => import('../components/layout/AppLayout.vue'),
    meta: { requiresAuth: true }, // 需要登录验证
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/HomeView.vue'),
        meta: {
          title: '首页',
          closable: false
        }
      },
      {
        path: '/user-list',
        name: 'user-list',
        component: () => import('@/views/UserManagement.vue'),
        meta: {
          title: '用户管理',
          permissions: ['SYSTEM:USER:VIEW']
        }
      },
      {
        path: '/role-management',
        name: 'role-management',
        component: () => import('@/views/RoleManagement.vue'),
        meta: {
          title: '角色管理',
          permissions: ['SYSTEM:USER:VIEW']
        }
      },
      {
        path: '/permission-management',
        name: 'permission-management',
        component: () => import('@/views/PermissionManagement.vue'),
        meta: {
          title: '权限管理',
          permissions: ['SYSTEM:USER:VIEW']
        }
      },
      {
        path: '/user-role-assignment',
        name: 'user-role-assignment',
        component: () => import('@/views/UserRoleAssignment.vue'),
        meta: {
          title: '用户角色分配',
          permissions: ['SYSTEM:USER:VIEW']
        }
      },
      {
        path: '/role-permission-assignment',
        name: 'role-permission-assignment',
        component: () => import('@/views/RolePermissionAssignment.vue'),
        meta: {
          title: '角色权限分配',
          permissions: ['SYSTEM:USER:VIEW']
        }
      },
      {
        path: '/contract-management',
        name: 'contract-management',
        component: () => import('@/views/ContractManagement.vue'),
        meta: {
          title: '合同管理',
          permissions: ['BUSINESS:CONTRACT:VIEW']
        }
      },
      {
        path: '/contract-payment-management',
        name: 'contract-payment-management',
        component: () => import('@/views/ContractPaymentManagement.vue'),
        meta: {
          title: '合同付款管理',
          permissions: ['BUSINESS:CONTRACT:VIEW']
        }
      },
      {
        path: '/archive-type-management',
        name: 'archive-type-management',
        component: () => import('@/views/ArchiveTypeManagement.vue'),
        meta: {
          title: '档案类型管理',
          permissions: ['BUSINESS:ARCHIVE:VIEW']
        }
      },
      {
        path: '/archive-management',
        name: 'archive-management',
        component: () => import('@/views/ArchiveManagement.vue'),
        meta: {
          title: '档案管理',
          permissions: ['BUSINESS:ARCHIVE:VIEW']
        }
      },
      {
        path: '/subject-management',
        name: 'subject-management',
        component: () => import('@/views/SubjectManagement.vue'),
        meta: {
          title: '科目管理',
          permissions: ['FINANCE:SUBJECT:VIEW']
        }
      },
      {
        path: '/bank-account-management',
        name: 'bank-account-management',
        component: () => import('@/views/BankAccountManagement.vue'),
        meta: {
          title: '银行账户管理',
          permissions: ['FINANCE:BANK:VIEW']
        }
      },
      {
        path: '/income-expense-management',
        name: 'income-expense-management',
        component: () => import('@/views/IncomeExpenseManagement.vue'),
        meta: {
          title: '收支管理',
          permissions: ['FINANCE:INCOME:VIEW']
        }
      },
      {
        path: '/cash-flow-management',
        name: 'cash-flow-management',
        component: () => import('@/views/CashFlowManagement.vue'),
        meta: {
          title: '出纳流水管理',
          permissions: ['FINANCE:CNLS:VIEW']
        }
      },
      {
        path: '/house-management',
        name: 'house-management',
        component: () => import('@/views/HouseManagement.vue'),
        meta: {
          title: '房源管理',
          permissions: ['SALES:HOUSE:VIEW']
        }
      },
      {
        path: '/cost-management',
        name: 'cost-management',
        component: () => import('@/views/CostManagement.vue'),
        meta: {
          title: '房屋成本管理',
          permissions: ['BUSINESS:COST:VIEW']
        }
      },
      {
        path: '/cost-detail-management/:fycbId?',
        name: 'cost-detail-management',
        component: () => import('@/views/CostDetailManagementNew.vue'),
        meta: { title: '房屋成本明细', permissions: ['BUSINESS:COST:VIEW'] }
      },
      {
        path: '/admin-panel',
        name: 'admin-panel',
        component: () => import('@/views/AdminPanel.vue'),
        meta: { 
          title: '管理员面板',
          permissions: ['ROLE_ADMIN']
        }
      },
      {
        path: '/test-table',
        name: 'test-table',
        component: () => import('@/views/TestTable.vue'),
        meta: {
          title: '表格测试',
          permissions: ['SYSTEM:USER:VIEW']
        }
      }
    ]
  },
  {
    path: '/forbidden',
    name: 'forbidden',
    component: () => import('@/views/Forbidden.vue'),
    meta: { 
      title: '无权限访问',
      requiresAuth: false
    }
  }
]

const router = createRouter({
  history: routerMode(import.meta.env.BASE_URL),
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const isLoggedIn = userStore.isLoggedIn

  // 不需要认证的路由
  if (!to.meta.requiresAuth) {
    return next()
  }

  // 未登录重定向
  if (!isLoggedIn) {
    return next({ name: 'login', query: { redirect: to.fullPath } })
  }

  // 权限检查 - 支持超级管理员
  if (to.meta.permissions) {
    // 超级管理员拥有所有权限
    if (userStore.isAdmin) {
      return next()
    }
    
    const hasPermission = to.meta.permissions.some(perm => 
      userStore.permissions && userStore.permissions.includes(perm))
    
    if (!hasPermission) {
      return next({ name: 'forbidden' })
    }
  }

  next()
})

export default router